#!/usr/bin/env python
# vim:fileencoding=utf-8
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''
Profile to download CNN
'''

from calibre.web.feeds.news import BasicNewsRecipe, classes


class CNN(BasicNewsRecipe):

    title = 'CNN'
    description = 'Global news'
    timefmt = ' [%d %b %Y]'
    __author__ = 'Kovid Goyal'
    language = 'en_US'

    no_stylesheets = True
    use_embedded_content = False
    oldest_article = 2
    ignore_duplicate_articles = {'url'}
    max_articles_per_feed = 25
    remove_attributes = ['style', 'height', 'width']
    keep_only_tags = [
        classes('headline__wrapper headline__sub-container article__main'),
    ]
    remove_tags = [classes('video-inline_carousel')]

    recipe_specific_options = {
        'days': {
            'short': 'Oldest article to download from this news source. In days ',
            'long': 'For example, 0.5, gives you articles from the past 12 hours',
            'default': str(oldest_article)
        }
    }

    def __init__(self, *args, **kwargs):
        BasicNewsRecipe.__init__(self, *args, **kwargs)
        d = self.recipe_specific_options.get('days')
        if d and isinstance(d, str):
            self.oldest_article = float(d)

    feeds = [
        ('Top News', 'http://rss.cnn.com/rss/cnn_topstories.rss'),
        ('World', 'http://rss.cnn.com/rss/cnn_world.rss'),
        ('U.S.', 'http://rss.cnn.com/rss/cnn_us.rss'),
        # ('Sports', 'http://rss.cnn.com/rss/si_topstories.rss'),
        ('Business', 'http://rss.cnn.com/rss/money_latest.rss'),
        ('Politics', 'http://rss.cnn.com/rss/cnn_allpolitics.rss'),
        ('Law', 'http://rss.cnn.com/rss/cnn_law.rss'),
        ('Technology', 'http://rss.cnn.com/rss/cnn_tech.rss'),
        ('Science & Space', 'http://rss.cnn.com/rss/cnn_space.rss'),
        ('Health', 'http://rss.cnn.com/rss/cnn_health.rss'),
        ('Entertainment', 'http://rss.cnn.com/rss/cnn_showbiz.rss'),
        ('Education', 'http://rss.cnn.com/rss/cnn_education.rss'),
        ('Offbeat', 'http://rss.cnn.com/rss/cnn_offbeat.rss'),
        ('Most Popular', 'http://rss.cnn.com/rss/cnn_mostpopular.rss')
    ]

    def get_article_url(self, article):
        ans = BasicNewsRecipe.get_article_url(self, article)
        ans = ans.partition('?')[0]
        if '.com/videos/' in ans:
            ans = None
        return ans

    def get_masthead_url(self):
        masthead = 'http://i.cdn.turner.com/cnn/.element/img/3.0/global/header/intl/hdr-globe-central.gif'
        br = BasicNewsRecipe.get_browser(self)
        try:
            br.open(masthead)
        except:
            self.log('\nCover unavailable')
            masthead = None
        return masthead

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'src':lambda x: x and x.endswith('.svg')}):
            img.extract()
        return soup
